const util = require('./util')
function upload(options) {
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            ...options,
            success: function (res) {
                console.log(res);
                let url = options.formData.aliyunServerURL + '/' + options.formData.key;
                if (res.errMsg === 'uploadFile:ok') {
                    resolve({
                        status: true,
                        data: {
                            url: url,
                        }
                    });
                }
            },
            fail: function (err) {
                reject({
                    status: false,
                    err,
                });
            },
        })
    })
}
export function uploadFile(filePath, remoteFilePath) {
    if (!filePath) {
        return {
            status: false,
            err: '文件错误',
        };
    }
    let dotIndex = filePath.lastIndexOf('.');
    let filePathLength = filePath.length;
    let suffix = filePath.substring(dotIndex, filePathLength);
    const aliyunFileKey = remoteFilePath + (new Date().getTime()) + suffix;
    console.log(suffix)
    return util.requestApi('getOssUploadKey', {
        aliyunFileKey: aliyunFileKey
    }).then(res => {
        let data = res.result;
        return upload({
            url: data.aliyunServerURL,
            filePath: filePath,
            name: 'file',
            formData: data
        })
    })
}